package com.software.levelupserver.service.impl;

import com.software.levelupserver.entity.Badge;
import com.software.levelupserver.entity.User;
import com.software.levelupserver.mappers.BadgeMapper;
import com.software.levelupserver.service.BadgeService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BadgeServiceImpl implements BadgeService {

    @Resource
    private BadgeMapper badgeMapper;

    /**
     * 能够获取安全认证之后的用户信息
     * @return
     */
    public User getAuthentication(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null){
            return (User) authentication.getPrincipal();
        }else {
            return null;
        }
    }

    /**
     * 获取用户获得的所有徽章
     * @return
     */
    @Override
    public List<Badge> getAllBadgeByUserId() {
        User user = getAuthentication();
        List<Badge> badgeList = null;
        if (user != null){
            return badgeMapper.selectAllBadgeByUserId(user.getUserId());
        } else
            return null;

//        return badgeList;
    }

    /**
     * 获取用户获得徽章中某种类型的最高等级徽章
     * @param badgeType
     * @return
     */
    @Override
    public Badge getBadgeByTypeAndUserId(int badgeType) {
        User user = getAuthentication();

        Badge badge = null;
        if (user != null)
            return badgeMapper.selectBadgeByTypeAndUserId(badgeType, user.getUserId());
        else
            return new Badge();

//        return badge;
    }

    @Override
    public List<Badge> getAllBadge() {
        List<Badge> badgeList = badgeMapper.selectAllBadge();

        return badgeList;
    }
}
